package com.thread.pool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestMainCallableInvoke {

	public static void main(String[] args) {

//		ExecutorService exec = Executors.newCachedThreadPool();
		ExecutorService exec = Executors.newFixedThreadPool(3);
		
		List<Callable<String>> tasks = new ArrayList<>();
		
		for (int i = 0; i < 10; i++) {
			tasks.add(new PrintCallable(i));
		}
		
		List<Future<String>> list = null;
		try {
			list = exec.invokeAll(tasks);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		exec.shutdown();
		
		for (Future<String> future : list) {
			try{
				
				if (!future.isDone())
					continue;
				
				System.out.println(future.get());
			} catch (Exception e) {
				exec.shutdownNow();
				e.printStackTrace();
			}
		}
		
	}

}
